﻿using System;
using System.Linq;
using System.Linq.Expressions;

namespace SimpleORM
{
    internal class ExpressionCheater : ExpressionVisitor
    {
        private IQueryable qc;
        private Type targetType;

        internal ExpressionCheater(IQueryable qc, Type typeTarget)
        {
            this.qc = qc;
            this.targetType = typeTarget;
        }

        internal Expression Cheat(Expression expression)
        {
            expression = this.Visit(expression);
            return expression;
        }

        protected override Expression VisitConstant(ConstantExpression c)
        {
            if (c.Type.IsKindOf(targetType))
                return Expression.Constant(this.qc);
            else
                return c;
        }
    }
}
